package cn.edu.scau.cmi.wujingxuan.singleton;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class SingletonClient extends Application implements EventHandler<ActionEvent>
{

	public static Chairman chariman = null;

	public static GridPane grid;

	@Override
	public void start(Stage primaryStage)
	{
		try
		{

			grid = new GridPane();
			grid.setPadding(new Insets(50, 25, 30, 25));
			grid.setHgap(10);
			grid.setVgap(5);
			Label chairmanLabel = new Label("输入主席名字：");
			grid.add(chairmanLabel, 0, 0);

			TextField chairmanName = new TextField(null);
			grid.add(chairmanName, 1, 0);

			Button create = new Button("创建主席");
			// button.setOnAction(this::handle);
			create.setOnAction(this);
			grid.add(create, 0, 1);

			Button clear = new Button("放弃/清空");
			grid.add(clear, 2, 1);

			Label chairmanLabel0 = new Label("刚创建的主席名为：");
			grid.add(chairmanLabel0, 0, 2);

			TextField chairmanName0 = new TextField(null);
			grid.add(chairmanName0, 1, 2);

			Label chairmanLabel1 = new Label(getName());
			grid.add(chairmanLabel1, 2, 2);

			// 创建主席
			create.setOnAction(e -> {

				if (chairmanName.getText() != null)
				{
					String name = chairmanName.getText();
					chariman = Chairman.getInstance(name);

					chairmanName0.setText(chariman.getName());
				}

			});

			// 放弃或者清空
			clear.setOnAction(e -> {

				chairmanName.setText(null);
				chairmanName0.setText(null);
			});

			BorderPane root = new BorderPane();
			root.setTop(grid);
			Scene scene = new Scene(root, 600, 400);
			primaryStage.setScene(scene);
			primaryStage.setTitle("单例工厂模式");
			primaryStage.show();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private String getName()
	{
		return null;
	}

	@Override
	public void handle(ActionEvent event)
	{
		System.out.println("Click button !");
	}

	public static void main(String[] args)
	{
		launch(args);
	}

}
